package com.sky.controller.admin;


import com.alibaba.fastjson.JSON;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderPage;
import com.sky.entity.Orders;
import com.sky.entity.TakeoutOrder;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import com.sky.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/order")
public class OrderController {

    @Autowired
    OrderService orderService;



    // 订单搜索
    @GetMapping("/conditionSearch")
    public Result searchOrder(OrdersPageQueryDTO ordersPageQueryDTO) {

        PageBean pageBean = orderService.searchOrder(ordersPageQueryDTO);

        return Result.success(pageBean);

    }

    // 派送订单
    @PutMapping("/delivery/{id}")
    public Result deliveryOrder(@PathVariable String id) {
        orderService.deliveryOrder(id);
        return Result.success();
    }

    //接单
    @PutMapping("/confirm")
    public Result confirmOrder(@RequestBody TakeoutOrder takeoutOrder) {
        orderService.confirmOrder(takeoutOrder);
        return Result.success();
    }

    //拒单
    @PutMapping("/rejection")
    public Result rejectOrder(@RequestBody TakeoutOrder takeoutOrder) {
        orderService.rejectOrder(takeoutOrder);
        return Result.success();
    }

    //查询订单详情
    @GetMapping("/details/{id}")

    public Result getDetails(@PathVariable Long id) {
        OrderVO orderById = orderService.getOrderById(id);
        return Result.success(orderById);
    }

    //取消订单
    @PutMapping("/cancel")
    public Result cancelOrder(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        orderService.cancelOrder(ordersCancelDTO);
        return Result.success();
    }

    // 各个状态的订单数量统计
    @GetMapping("/statistics")
    public Result countTheNumberOfOrders() {
        OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();
        orderStatisticsVO.setConfirmed(orderService.countConfirned());
        orderStatisticsVO.setToBeConfirmed(orderService.countToBeConfirmed());
        orderStatisticsVO.setDeliveryInProgress(orderService.countDeliveryInProgress());

        return Result.success(orderStatisticsVO);
    }

    // 完成订单
    @PutMapping("/complete/{id}")
    public Result completeOrder(@PathVariable String id){
        orderService.completeOrder(id);
        return Result.success();
    }


}
